From c0c759c027762081009019aa80f8c135baec9184 Mon Sep 17 00:00:00 2001 From: robertl Date: Thu, 2 Jan 2003 04:07:24 +0000 Subject: [PATCH] GPX: Count size of entity replacements instead of divining it. Magproto: More thoroughly close Windows handles. From Mottram. --- gpsbabel/gpx.c | 29 +++++++++++++++++++++++------ gpsbabel/magproto.c | 21 ++++++++++++++++----- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/gpsbabel/gpx.c b/gpsbabel/gpx.c index 520f3c2ea..b73c84302 100644 --- a/gpsbabel/gpx.c +++ b/gpsbabel/gpx.c @@ -58,8 +58,9 @@ static FILE *ofd; static char * gpx_entitize(const char * str) { - int elen; + int elen, ecount; const char ** ep; + const char * cp; char * p, * tmp, * xstr; const char * stdentities[] = { "&", "&", @@ -69,12 +70,28 @@ char * gpx_entitize(const char * str) "\"", """, NULL, NULL }; + ep = stdentities; + elen = ecount = 0; - /* enough space for the whole string at max entity size */ - /* i.e. "'" == "'" */ - tmp = xcalloc(((strlen(str) * 6) + 1), 1); + /* figure # of entity replacements and additional size. */ + while (*ep) { + cp = str; + while ((cp = strstr(cp, *ep)) != NULL) { + elen += strlen(*(ep + 1)) - strlen(*ep); + ecount++; + cp += strlen(*ep); + } + ep += 2; + } + + /* enough space for the whole string plus entity replacements, if any */ + tmp = xcalloc((strlen(str) + elen + 1), 1); strcpy(tmp, str); + /* no entity replacements */ + if (ecount == 0) + return (tmp); + ep = stdentities; while (*ep) { @@ -82,10 +99,10 @@ char * gpx_entitize(const char * str) while ((p = strstr(p, *ep)) != NULL) { elen = strlen(*(ep + 1)); - xstr = xstrdup(p + 1); + xstr = xstrdup(p + strlen(*ep)); + strcpy(p, *(ep + 1)); strcpy(p + elen, xstr); - memcpy(p, *(ep + 1), elen); free(xstr); diff --git a/gpsbabel/magproto.c b/gpsbabel/magproto.c index f4dc054e5..a8cccb08a 100644 --- a/gpsbabel/magproto.c +++ b/gpsbabel/magproto.c @@ -430,7 +430,9 @@ mkspeed(bitrate) } } -HANDLE comport; +HANDLE comport = NULL; + +#define xCloseHandle(a) if (a) { CloseHandle(a); } a = NULL; static int @@ -439,7 +441,11 @@ terminit(const char *portname) DCB tio; COMMTIMEOUTS timeout; - comport = CreateFile(portname, GENERIC_READ|GENERIC_WRITE, 0, NULL, + is_file = 0; + + xCloseHandle(comport); + + comport = CreateFile(portname, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (comport == INVALID_HANDLE_VALUE) { @@ -467,7 +473,7 @@ terminit(const char *portname) tio.StopBits = ONESTOPBIT; if (!SetCommState (comport, &tio)) { - CloseHandle(comport); + xCloseHandle(comport); /* * Probably not a com port. Try it as a file. @@ -489,7 +495,7 @@ terminit(const char *portname) timeout.WriteTotalTimeoutMultiplier = 10; timeout.WriteTotalTimeoutConstant = 1000; if (!SetCommTimeouts (comport, &timeout)) { - CloseHandle (comport); + xCloseHandle (comport); fatal(MYNAME ": set timeouts\n"); } return 1; @@ -535,7 +541,7 @@ static void termdeinit() { - CloseHandle(comport); + xCloseHandle(comport); } #else @@ -693,6 +699,11 @@ mag_wr_init(const char *portname, const char *args) if (magfile_out) { fclose(magfile_out); } +#if __WIN32__ + if (comport) { + xCloseHandle(comport); + } +#endif mag_rd_init(portname, args); } } -- 2.30.2